package czp.xml;

import java.util.List;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import czp.model.Mp3Info;

public class Mp3ListContentHandler extends DefaultHandler {
	private List<Mp3Info> infos = null;
	private Mp3Info mp3Info = null;
	private String tagName = null;
	
	public Mp3ListContentHandler(List<Mp3Info> infos) {
		super();
		this.infos = infos;
	}

	public List<Mp3Info> getInfos() {
		return infos;
	}

	public void setInfos(List<Mp3Info> infos) {
		this.infos = infos;
	}

	@Override
	public void startDocument() throws SAXException {
		super.startDocument();
	}
	
	@Override
	public void startElement(String uri, String localName, String qName,
			Attributes attributes) throws SAXException {
		this.tagName = localName;
		if("resource".equals(tagName)) {
			mp3Info = new Mp3Info();
		}
	}
	
	@Override
	public void characters(char[] ch, int start, int length)
			throws SAXException {
		String temp = new String(ch, start, length).trim();
		if("id".equals(tagName)) {
			mp3Info.setId(temp);
		} else if("mp3.name".equals(tagName)) {
			mp3Info.setMp3Name(temp);
		} else if("mp3.size".equals(tagName)) {
			mp3Info.setMp3Size(temp);
		} else if("lrc.name".equals(tagName)) {
			mp3Info.setLrcName(temp);
		} else if("lrc.size".equals(tagName)) {
			mp3Info.setLrcSize(temp);
		}
	}

	@Override
	public void endElement(String uri, String localName, String qName)
			throws SAXException {
		if("resource".equals(qName)) {
			infos.add(mp3Info);
		}
		tagName = null;
	}
	
	@Override
	public void endDocument() throws SAXException {
		super.endDocument();
	}


}
